﻿#include "Settings.h"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <Windows.h>
#include <iostream>

// 加密密钥
const std::string ENCRYPTION_KEY = "SyHexConfigEncryptionKey123!";

// Settings.cpp 文件顶部添加：
std::vector<std::string> cfgFiles;
int selectedCfgIndex = -1;
char newCfgName[128] = "";
std::string currentlyLoadedConfig = "";

// 在全局变量中添加
bool showLoadSuccess = false;
float loadSuccessAlpha = 1.0f;
std::chrono::steady_clock::time_point loadSuccessTime;
// 在全局变量初始化部分添加
bool showSaveSuccess = false;
float saveSuccessAlpha = 1.0f;
std::chrono::steady_clock::time_point saveSuccessTime;

// 简单的XOR加密函数
std::string encryptDecrypt(const std::string& input) {
    std::string output = input;
    for (size_t i = 0; i < input.size(); ++i) {
        output[i] = input[i] ^ ENCRYPTION_KEY[i % ENCRYPTION_KEY.size()];
    }
    return output;
}

// 辅助函数：将ImColor转换为字符串
std::string imColorToString(const ImColor& color) {
    std::ostringstream oss;
    oss << color.Value.x << "," << color.Value.y << "," << color.Value.z << "," << color.Value.w;
    return oss.str();
}

// 辅助函数：从字符串解析ImColor
ImColor stringToImColor(const std::string& str) {
    ImColor color;
    std::istringstream iss(str);
    char comma;
    iss >> color.Value.x >> comma >> color.Value.y >> comma >> color.Value.z >> comma >> color.Value.w;
    return color;
}

std::unordered_map<std::string, KeySelectorState> key_selector_states;

namespace all_settings {
    namespace hotkey_manager {
        struct HotkeyInfo {
            std::string name;
            WPARAM key;
            std::string description;
            bool enabled;

            // 添加指向实际变量的指针
            WPARAM* bound_variable = nullptr;
        };

        inline std::unordered_map<std::string, HotkeyInfo> hotkey_list;

        // 初始化函数现在自动绑定变量
        inline void init_hotkeys() {
            hotkey_list = {
                {"aimbot_toggle", {"Aimbot开关", all_settings::aimbot_anitaimbot::aimbot::open_aimbot.hotkey,
                                  "切换自瞄功能的开关状态", true, &all_settings::aimbot_anitaimbot::aimbot::open_aimbot.hotkey}},

                {"aimbot_target_bot", {"目标机器人", all_settings::aimbot_anitaimbot::Target_Bot::target_bot.hotkey,
                                     "目标机器人功能", true, &all_settings::aimbot_anitaimbot::Target_Bot::target_bot.hotkey}},


                {"menu_toggle", {"菜单开关", all_settings::skin::menu_steeing::menu_kjj,
                               "显示/隐藏菜单", true, &all_settings::skin::menu_steeing::menu_kjj}},

                {"thirdperson_toggle", {"第三人称开关", all_settings::visual::local::self::ThirdPerson.hotkey,
                                      "切换第三人称视角", true, &all_settings::visual::local::self::ThirdPerson.hotkey}}
            };
        }

        inline bool check_hotkey_conflict(WPARAM new_key, const std::string& exclude_id) {
            if (new_key == 0 || new_key == VK_ESCAPE) return false;

            for (const auto& [id, hotkey] : hotkey_list) {
                if (id != exclude_id && hotkey.key == new_key && hotkey.enabled) {
                    return true;
                }
            }
            return false;
        }

        inline void resolve_hotkey_conflict(WPARAM conflicting_key, const std::string& current_id) {
            for (auto& [id, hotkey] : hotkey_list) {
                if (id != current_id && hotkey.key == conflicting_key && hotkey.enabled) {
                    hotkey.key = 0;
                    if (hotkey.bound_variable) {
                        *hotkey.bound_variable = 0;
                    }
                }
            }
        }

        inline void update_hotkey(const std::string& id, WPARAM new_key) {
            auto it = hotkey_list.find(id);
            if (it == hotkey_list.end()) return;

            auto& hotkey = it->second;
            hotkey.key = new_key;
            if (hotkey.bound_variable) {
                *hotkey.bound_variable = new_key;
            }
        }

        inline std::string get_key_name(WPARAM key) {
            auto it = KEY_NAMES.find(key);
            return it != KEY_NAMES.end() ? it->second : "无";
        }

        inline bool is_key_pressed(WPARAM key) {
            return key != 0 && (GetAsyncKeyState(key) & 0x8000);
        }
    }
}

void KeySelector(const char* id, const char* name, WPARAM& current_key, const std::string& description) {
    ImGui::PushID(id);

    // 获取或创建状态
    KeySelectorState& state = key_selector_states[id];

    // 确保快捷键已注册
    if (all_settings::hotkey_manager::hotkey_list.find(id) ==
        all_settings::hotkey_manager::hotkey_list.end()) {
        all_settings::hotkey_manager::hotkey_list[id] = {
            name, current_key, description, true, &current_key
        };
    }

    // 显示当前键位名称
    std::string current_key_name = "无";
    if (current_key != 0 && KEY_NAMES.find(current_key) != KEY_NAMES.end()) {
        current_key_name = KEY_NAMES.at(current_key);
    }

    // 渲染按钮
    std::string button_text = state.is_selecting ? "..." : current_key_name;
    if (ImGui::Button(button_text.c_str(), ImVec2(80, 0))) {
        state.is_selecting = true;
        state.selected_key = current_key;

        // 取消其他选择器的状态
        for (auto& pair : key_selector_states) {
            if (pair.first != id) {
                pair.second.is_selecting = false;
            }
        }
    }

    // 显示工具提示
    if (ImGui::IsItemHovered() && !description.empty()) {
        ImGui::SetTooltip("%s", description.c_str());
    }

    // 处理键位选择结果
    if (state.key_selected) {
        // 检查冲突并解决
        if (all_settings::hotkey_manager::check_hotkey_conflict(state.selected_key, id)) {
            all_settings::hotkey_manager::resolve_hotkey_conflict(state.selected_key, id);
        }

        // 更新键位
        current_key = state.selected_key;
        all_settings::hotkey_manager::update_hotkey(id, state.selected_key);

        // 重置状态
        state.key_selected = false;
        state.is_selecting = false;
    }

    ImGui::PopID();
}

void CheckKeySelection() {
    for (auto& pair : key_selector_states) {
        KeySelectorState& state = pair.second;

        if (state.is_selecting) {
            // 检查所有可能的按键
            for (const auto& key_pair : KEY_NAMES) {
                if (GetAsyncKeyState(key_pair.first) & 0x8000) {
                    // 设置新键位
                    state.selected_key = key_pair.first;
                    state.key_selected = true;
                    state.is_selecting = false;
                    break;
                }
            }

            // ESC键取消选择
            if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
                state.is_selecting = false;
            }
        }
    }
}

void scanCfgFiles() {
    cfgFiles.clear();

    // 确保目录存在
    CreateDirectoryA("C:\\Sy_Hex", NULL);

    WIN32_FIND_DATAA findData;
    HANDLE hFind = FindFirstFileA("C:\\Sy_Hex\\*.cfg", &findData);

    if (hFind == INVALID_HANDLE_VALUE) {
        DWORD error = GetLastError();
        return;
    }

    do {
        if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
            cfgFiles.push_back(findData.cFileName);
        }
    } while (FindNextFileA(hFind, &findData));

    FindClose(hFind);
}

// 保存配置（带加密）
void saveConfig(const std::string& filename) {
    // 确保目录存在
    CreateDirectoryA("C:\\Sy_Hex", NULL);

    std::stringstream buffer;

    // 保存全局设置
    buffer << "[Global]\n";
    buffer << "windowscale=" << all_settings::setting::windowscale << "\n";
    // 保存Aimbot设置
    buffer << "[Aimbot]\n";
    buffer << "open_aimbot=" << (all_settings::aimbot_anitaimbot::aimbot::open_aimbot.enabled ? "1" : "0") << "\n";
    buffer << "open_aimbot_mode=" << static_cast<int>(all_settings::aimbot_anitaimbot::aimbot::open_aimbot.mode) << "\n";
    buffer << "open_aimbot_hotkey=" << static_cast<int>(all_settings::aimbot_anitaimbot::aimbot::open_aimbot.hotkey) << "\n";
    buffer << "autofire=" << (all_settings::aimbot_anitaimbot::aimbot::autofire ? "1" : "0") << "\n";
    buffer << "autofiree_yc=" << all_settings::aimbot_anitaimbot::aimbot::autofiree_yc << "\n";
    buffer << "gqs=" << (all_settings::aimbot_anitaimbot::aimbot::gqs ? "1" : "0") << "\n";
    buffer << "autostop=" << (all_settings::aimbot_anitaimbot::aimbot::autostop ? "1" : "0") << "\n";
    buffer << "autoScoped=" << (all_settings::aimbot_anitaimbot::aimbot::autoScoped ? "1" : "0") << "\n";
    buffer << "team_pd=" << (all_settings::aimbot_anitaimbot::aimbot::team_pd ? "1" : "0") << "\n";
    buffer << "current_aim_mode=" << static_cast<int>(all_settings::aimbot_anitaimbot::aimbot::current_aim_mode) << "\n";

    // 保存Aimbot武器设置
    for (const auto& weapon : all_settings::aimbot_anitaimbot::aimbot::aimbot_weapon_settings) {
        buffer << "\n[AimbotWeapon_" << weapon.first << "]\n";
        buffer << "enabled=" << (weapon.second.enabled ? "1" : "0") << "\n";
        buffer << "fov=" << weapon.second.fov << "\n";
        buffer << "aimbot_smooth=" << weapon.second.aimbot_smooth << "\n";
        buffer << "autowall=" << (weapon.second.autowall ? "1" : "0") << "\n";
        buffer << "autowall_fw=" << weapon.second.autowall_fw << "\n";
        buffer << "priority_bone=" << weapon.second.priority_bone << "\n";
        buffer << "bone_head=" << (weapon.second.bone_settings.head ? "1" : "0") << "\n";
        buffer << "bone_neck=" << (weapon.second.bone_settings.neck ? "1" : "0") << "\n";
        buffer << "bone_spine=" << (weapon.second.bone_settings.spine ? "1" : "0") << "\n";
        buffer << "bone_pelvis=" << (weapon.second.bone_settings.pelvis ? "1" : "0") << "\n";
        buffer << "bone_armUpper_L=" << (weapon.second.bone_settings.armUpper_L ? "1" : "0") << "\n";
        buffer << "bone_armUpper_R=" << (weapon.second.bone_settings.armUpper_R ? "1" : "0") << "\n";
        buffer << "bone_legUpper_L=" << (weapon.second.bone_settings.legUpper_L ? "1" : "0") << "\n";
        buffer << "bone_legUpper_R=" << (weapon.second.bone_settings.legUpper_R ? "1" : "0") << "\n";
    }

    // 保存TargetBot设置
    buffer << "\n[TargetBot]\n";
    buffer << "target_bot=" << (all_settings::aimbot_anitaimbot::Target_Bot::target_bot.enabled ? "1" : "0") << "\n";
    buffer << "target_bot_mode=" << static_cast<int>(all_settings::aimbot_anitaimbot::Target_Bot::target_bot.mode) << "\n";
    buffer << "target_bot_hotkey=" << static_cast<int>(all_settings::aimbot_anitaimbot::Target_Bot::target_bot.hotkey) << "\n";
    buffer << "auto_knife=" << (all_settings::aimbot_anitaimbot::Target_Bot::auto_knife ? "1" : "0") << "\n";
    buffer << "auto_taser=" << (all_settings::aimbot_anitaimbot::Target_Bot::auto_taser ? "1" : "0") << "\n";

    // 保存TargetBot武器设置
    for (const auto& weapon : all_settings::aimbot_anitaimbot::Target_Bot::target_bot_weapon_settings) {
        buffer << "\n[TargetBotWeapon_" << weapon.first << "]\n";
        buffer << "enabled=" << (weapon.second.enabled ? "1" : "0") << "\n";
        buffer << "target_bot_yc=" << weapon.second.target_bot_yc << "\n";
    }

    // 保存Weapon_RCS设置
    buffer << "\n[WeaponRCS]\n";
    buffer << "Open_Weapon_RCS=" << (all_settings::aimbot_anitaimbot::Weapon_RCS::Open_Weapon_RCS ? "1" : "0") << "\n";

    // 保存Weapon_RCS武器设置
    for (const auto& weapon : all_settings::aimbot_anitaimbot::Weapon_RCS::Weapon_RCS_settings) {
        buffer << "\n[WeaponRCSWeapon_" << weapon.first << "]\n";
        buffer << "enabled=" << (weapon.second.enabled ? "1" : "0") << "\n";
        buffer << "vertical=" << weapon.second.vertical << "\n";
        buffer << "horizontal=" << weapon.second.horizontal << "\n";
        buffer << "rcs_smooth=" << weapon.second.rcs_smooth << "\n";
    }



    // 保存Movement设置
    buffer << "\n[Movement]\n";
    buffer << "bunny_bhop=" << (all_settings::skin::movement::bunny_bhop ? "1" : "0") << "\n";
    buffer << "auto_space_turn=" << (all_settings::skin::movement::auto_space_turn ? "1" : "0") << "\n";
    buffer << "slowwalk=" << (all_settings::skin::movement::slowwalk ? "1" : "0") << "\n";
    buffer << "slowwalk_speed=" << all_settings::skin::movement::slowwalk_speed << "\n";
    buffer << "standalone_quick_stop=" << (all_settings::skin::movement::standalone_quick_stop ? "1" : "0") << "\n";

    // 保存Voice设置
    buffer << "\n[Voice]\n";
    buffer << "hit_sound=" << (all_settings::skin::voice::hit_sound ? "1" : "0") << "\n";
    buffer << "hit_message=" << (all_settings::skin::voice::hit_message ? "1" : "0") << "\n";
    buffer << "hit_sound_index=" << all_settings::skin::voice::hit_sound_index << "\n";
    buffer << "hit_death_message=" << (all_settings::skin::voice::hit_death_message ? "1" : "0") << "\n";
    buffer << "killerName=" << all_settings::skin::voice::killerName << "\n";
    buffer << "weaponName=" << all_settings::skin::voice::weaponName << "\n";

    // 保存Speech设置
    buffer << "\n[Speech]\n";
    buffer << "open_Speech=" << (all_settings::skin::Speech::open_Speech ? "1" : "0") << "\n";
    buffer << "Speech_mode=" << static_cast<int>(all_settings::skin::Speech::Speech_mode) << "\n";

    // 保存语音内容
    buffer << "savedContents=";
    bool firstItem = true;
    for (const auto& content : all_settings::skin::Speech::savedContents) {
        if (!firstItem) buffer << "|";
        buffer << content;
        firstItem = false;
    }
    if (all_settings::skin::Speech::savedContents.empty()) {
        buffer << "EMPTY";
    }
    buffer << "\n";

    // 保存Menu设置
    buffer << "\n[Menu]\n";
    buffer << "menu_kjj=" << static_cast<int>(all_settings::skin::menu_steeing::menu_kjj) << "\n";

    // 保存Visual设置
    buffer << "\n[Visual]\n";
    buffer << "ESP=" << (all_settings::visual::ESP ? "1" : "0") << "\n";
    buffer << "text_font=" << all_settings::visual::text_font << "\n";
    // 保存Enemy Visual设置
    buffer << "\n[EnemyVisual]\n";
    buffer << "enemy_visual=" << (all_settings::visual::enemy::enemy_visual ? "1" : "0") << "\n";
    buffer << "enemy_kuang_visual=" << (all_settings::visual::enemy::enemy_kuang_visual ? "1" : "0") << "\n";
    buffer << "enemy_kuang_visual_color_ks=" << imColorToString(all_settings::visual::enemy::enemy_kuang_visual_color_ks) << "\n";
    buffer << "enemy_name_visual=" << (all_settings::visual::enemy::enemy_name_visual ? "1" : "0") << "\n";
    buffer << "enemy_name_visual_color=" << imColorToString(all_settings::visual::enemy::enemy_name_visual_color) << "\n";
    buffer << "enemy_ShowHealth_visual=" << (all_settings::visual::enemy::enemy_ShowHealth_visual ? "1" : "0") << "\n";
    buffer << "enemy_Health_color=" << imColorToString(all_settings::visual::enemy::enemy_Health_color) << "\n";
    buffer << "enemy_weapon_visual=" << (all_settings::visual::enemy::enemy_weapon_visual ? "1" : "0") << "\n";
    buffer << "enemy_boon_visual=" << (all_settings::visual::enemy::enemy_boon_visual ? "1" : "0") << "\n";
    buffer << "enemy_boon_visual_color=" << imColorToString(all_settings::visual::enemy::enemy_boon_visual_color) << "\n";
    buffer << "enemy_visual_color=" << (all_settings::visual::enemy::enemy_visual_color ? "1" : "0") << "\n";
    buffer << "enemy_weapon_glow=" << (all_settings::visual::enemy::enemy_weapon_glow ? "1" : "0") << "\n";
    buffer << "enemy_weapon_glow_color=" << imColorToString(all_settings::visual::enemy::enemy_weapon_glow_color) << "\n";
    buffer << "enemy_distance=" << (all_settings::visual::enemy::enemy_distance ? "1" : "0") << "\n";


    // 保存Teammate Visual设置
    buffer << "\n[TeammateVisual]\n";
    buffer << "teammate_visual=" << (all_settings::visual::teammate::teammate_visual ? "1" : "0") << "\n";
    buffer << "teammate_kuang_visual=" << (all_settings::visual::teammate::teammate_kuang_visual ? "1" : "0") << "\n";
    buffer << "teammate_kuang_visual_color_ks=" << imColorToString(all_settings::visual::teammate::teammate_kuang_visual_color_ks) << "\n";
    buffer << "teammate_name_visual=" << (all_settings::visual::teammate::teammate_name_visual ? "1" : "0") << "\n";
    buffer << "teammate_name_visual_color=" << imColorToString(all_settings::visual::teammate::teammate_name_visual_color) << "\n";
    buffer << "teammate_ShowHealth_visual=" << (all_settings::visual::teammate::teammate_ShowHealth_visual ? "1" : "0") << "\n";
    buffer << "teammate_Health_color=" << imColorToString(all_settings::visual::teammate::teammate_Health_color) << "\n";
    buffer << "teammate_weapon_visual=" << (all_settings::visual::teammate::teammate_weapon_visual ? "1" : "0") << "\n";
    buffer << "teammate_boon_visual=" << (all_settings::visual::teammate::teammate_boon_visual ? "1" : "0") << "\n";
    buffer << "teammate_boon_visual_color=" << imColorToString(all_settings::visual::teammate::teammate_boon_visual_color) << "\n";
    buffer << "teammate_visual_color=" << (all_settings::visual::teammate::teammate_visual_color ? "1" : "0") << "\n";
    buffer << "teammate_weapon_glow=" << (all_settings::visual::teammate::teammate_weapon_glow ? "1" : "0") << "\n";
    buffer << "teammate_weapon_glow_color=" << imColorToString(all_settings::visual::teammate::teammate_weapon_glow_color) << "\n";
    buffer << "teammate_distance=" << (all_settings::visual::teammate::teammate_distance ? "1" : "0") << "\n";


    // 保存Local Visual设置
    buffer << "\n[LocalVisual]\n";
    buffer << "local_glow=" << (all_settings::visual::local::self::local_glow ? "1" : "0") << "\n";
    buffer << "player_fov=" << (all_settings::visual::local::self::player_fov ? "1" : "0") << "\n";
    buffer << "player_fov_fw=" << all_settings::visual::local::self::player_fov_fw << "\n";
    buffer << "weapon_glow=" << (all_settings::visual::local::self::weapon_glow ? "1" : "0") << "\n";
    buffer << "weapon_glow_color=" << imColorToString(all_settings::visual::local::self::weapon_glow_color) << "\n";
    buffer << "show_crosshair=" << (all_settings::visual::local::self::show_crosshair ? "1" : "0") << "\n";
    buffer << "crosshair_Length=" << all_settings::visual::local::self::crosshair_Length << "\n";
    buffer << "crosshair_gap=" << all_settings::visual::local::self::crosshair_gap << "\n";
    buffer << "crosshair_center_lenth=" << all_settings::visual::local::self::crosshair_center_lenth << "\n";
    buffer << "crosshair_minThickness=" << all_settings::visual::local::self::crosshair_minThickness << "\n";
    buffer << "crosshair_maxThickness=" << all_settings::visual::local::self::crosshair_maxThickness << "\n";
    buffer << "crosshair_color_1=" << imColorToString(all_settings::visual::local::self::crosshair_color_1) << "\n";
    buffer << "crosshair_color_2=" << imColorToString(all_settings::visual::local::self::crosshair_color_2) << "\n";
    buffer << "Scoped_fov=" << all_settings::visual::local::self::Scoped_fov << "\n";
    buffer << "remove_Scoped_fov=" << (all_settings::visual::local::self::remove_Scoped_fov ? "1" : "0") << "\n";
    buffer << "remove_Scoped_blachk=" << (all_settings::visual::local::self::remove_Scoped_blachk ? "1" : "0") << "\n";
    buffer << "ThirdPerson=" << (all_settings::visual::local::self::ThirdPerson.enabled ? "1" : "0") << "\n";
    buffer << "targe_hole_ThirdPerson=" << static_cast<int>(all_settings::visual::local::self::ThirdPerson.mode) << "\n";
    buffer << "ThirdPerson_kjj=" << static_cast<int>(all_settings::visual::local::self::ThirdPerson.hotkey) << "\n";

    // 保存World Visual设置
    buffer << "\n[WorldVisual]\n";
    buffer << "draw_world_color=" << (all_settings::visual::local::world::draw_world_color ? "1" : "0") << "\n";
    buffer << "world_color=" << imColorToString(all_settings::visual::local::world::world_color) << "\n";
    buffer << "draw_fov=" << (all_settings::visual::local::world::draw_fov ? "1" : "0") << "\n";
    buffer << "draw_C4_time=" << (all_settings::visual::local::world::draw_C4_time ? "1" : "0") << "\n";
    buffer << "fov_color=" << imColorToString(all_settings::visual::local::world::fov_color) << "\n";
    buffer << "remove_3d_skybos=" << (all_settings::visual::local::world::remove_3d_skybos ? "1" : "0") << "\n";

    // 保存Grenade Removal设置
    buffer << "\n[GrenadeRemoval]\n";
    buffer << "removeFlash=" << (all_settings::visual::local::world::grenade_settings.removeFlash ? "1" : "0") << "\n";
    buffer << "removeSmoke=" << (all_settings::visual::local::world::grenade_settings.removeSmoke ? "1" : "0") << "\n";
    buffer << "removeBlood=" << (all_settings::visual::local::world::grenade_settings.removeBlood ? "1" : "0") << "\n";
    buffer << "removeBulletImpacts=" << (all_settings::visual::local::world::grenade_settings.removeBulletImpacts ? "1" : "0") << "\n";
    buffer << "removeShellCasings=" << (all_settings::visual::local::world::grenade_settings.removeShellCasings ? "1" : "0") << "\n";

    // 保存World Removal设置
    buffer << "\n[WorldRemoval]\n";
    buffer << "removeShadows=" << (all_settings::visual::local::world::world_settings.removeShadows ? "1" : "0") << "\n";
    buffer << "removeWorldLighting=" << (all_settings::visual::local::world::world_settings.removeWorldLighting ? "1" : "0") << "\n";
    buffer << "removeFog=" << (all_settings::visual::local::world::world_settings.removeFog ? "1" : "0") << "\n";
    buffer << "removeWeather=" << (all_settings::visual::local::world::world_settings.removeWeather ? "1" : "0") << "\n";
    buffer << "removeParticles=" << (all_settings::visual::local::world::world_settings.removeParticles ? "1" : "0") << "\n";

    // 加密并写入文件
    std::string encryptedData = encryptDecrypt(buffer.str());
    std::ofstream file("C:\\Sy_Hex\\" + filename + ".cfg", std::ios::binary);
    if (file.is_open()) {
        file.write(encryptedData.c_str(), encryptedData.size());
        file.close();

        // 设置保存成功提示
        showSaveSuccess = true;
        saveSuccessAlpha = 1.0f;
        saveSuccessTime = std::chrono::steady_clock::now();

        // 重新扫描配置文件
        scanCfgFiles();
    }
}

// 加载配置（带解密）
void loadConfig(const std::string& filename) {
    std::ifstream file("C:\\Sy_Hex\\" + filename + ".cfg", std::ios::binary | std::ios::ate);
    if (!file.is_open()) {
        std::cout << "无法打开配置文件: " << filename << std::endl;
        return;
    }

    // 读取整个文件内容
    std::streamsize size = file.tellg();
    file.seekg(0, std::ios::beg);
    std::string encryptedData(size, '\0');
    if (file.read(&encryptedData[0], size)) {
        // 解密数据
        std::string decryptedData = encryptDecrypt(encryptedData);
        std::istringstream iss(decryptedData);

        std::string line;
        std::string currentSection;

        while (std::getline(iss, line)) {
            if (line.empty()) continue;

            if (line[0] == '[' && line.back() == ']') {
                currentSection = line.substr(1, line.size() - 2);
                continue;
            }

            size_t eqPos = line.find('=');
            if (eqPos == std::string::npos) continue;

            std::string key = line.substr(0, eqPos);
            std::string value = line.substr(eqPos + 1);

            try {
                if (currentSection == "Global") {
                    if (key == "windowscale") all_settings::setting::windowscale = std::stof(value);
                }
                if (currentSection == "Aimbot") {
                    if (key == "open_aimbot") all_settings::aimbot_anitaimbot::aimbot::open_aimbot.enabled = (value == "1");
                    else if (key == "open_aimbot_mode") all_settings::aimbot_anitaimbot::aimbot::open_aimbot.mode = static_cast<all_settings::targe_hole>(std::stoi(value));
                    else if (key == "open_aimbot_hotkey") all_settings::aimbot_anitaimbot::aimbot::open_aimbot.hotkey = static_cast<WPARAM>(std::stoul(value));
                    else if (key == "autofire") all_settings::aimbot_anitaimbot::aimbot::autofire = (value == "1");
                    else if (key == "autofiree_yc") all_settings::aimbot_anitaimbot::aimbot::autofiree_yc = std::stof(value);
                    else if (key == "gqs") all_settings::aimbot_anitaimbot::aimbot::gqs = (value == "1");
                    else if (key == "autostop") all_settings::aimbot_anitaimbot::aimbot::autostop = (value == "1");
                    else if (key == "autoScoped") all_settings::aimbot_anitaimbot::aimbot::autoScoped = (value == "1");
                    else if (key == "team_pd") all_settings::aimbot_anitaimbot::aimbot::team_pd = (value == "1");
                    else if (key == "current_aim_mode") all_settings::aimbot_anitaimbot::aimbot::current_aim_mode = static_cast<all_settings::AimMode>(std::stoi(value));
                }
                else if (currentSection == "TargetBot") {
                    if (key == "target_bot") all_settings::aimbot_anitaimbot::Target_Bot::target_bot.enabled = (value == "1");
                    else if (key == "target_bot_mode") all_settings::aimbot_anitaimbot::Target_Bot::target_bot.mode = static_cast<all_settings::targe_hole>(std::stoi(value));
                    else if (key == "target_bot_hotkey") all_settings::aimbot_anitaimbot::Target_Bot::target_bot.hotkey = static_cast<WPARAM>(std::stoul(value));
                    else if (key == "auto_knife") all_settings::aimbot_anitaimbot::Target_Bot::auto_knife = (value == "1");
                    else if (key == "auto_taser") all_settings::aimbot_anitaimbot::Target_Bot::auto_taser = (value == "1");
                }
                else if (currentSection == "WeaponRCS") {
                    if (key == "Open_Weapon_RCS") all_settings::aimbot_anitaimbot::Weapon_RCS::Open_Weapon_RCS = (value == "1");
                }
                else if (currentSection.find("AimbotWeapon_") == 0) {
                    int weaponId = std::stoi(currentSection.substr(13));
                    auto& settings = all_settings::aimbot_anitaimbot::aimbot::aimbot_weapon_settings[weaponId];

                    if (key == "enabled") settings.enabled = (value == "1");
                    else if (key == "fov") settings.fov = std::stof(value);
                    else if (key == "aimbot_smooth") settings.aimbot_smooth = std::stof(value);
                    else if (key == "autowall") settings.autowall = (value == "1");
                    else if (key == "autowall_fw") settings.autowall_fw = std::stof(value);
                    else if (key == "priority_bone") settings.priority_bone = std::stoi(value);
                    else if (key == "bone_head") settings.bone_settings.head = (value == "1");
                    else if (key == "bone_neck") settings.bone_settings.neck = (value == "1");
                    else if (key == "bone_spine") settings.bone_settings.spine = (value == "1");
                    else if (key == "bone_pelvis") settings.bone_settings.pelvis = (value == "1");
                    else if (key == "bone_armUpper_L") settings.bone_settings.armUpper_L = (value == "1");
                    else if (key == "bone_armUpper_R") settings.bone_settings.armUpper_R = (value == "1");
                    else if (key == "bone_legUpper_L") settings.bone_settings.legUpper_L = (value == "1");
                    else if (key == "bone_legUpper_R") settings.bone_settings.legUpper_R = (value == "1");
                }
                else if (currentSection.find("TargetBotWeapon_") == 0) {
                    int weaponId = std::stoi(currentSection.substr(16));
                    auto& settings = all_settings::aimbot_anitaimbot::Target_Bot::target_bot_weapon_settings[weaponId];

                    if (key == "enabled") settings.enabled = (value == "1");
                    else if (key == "target_bot_yc") settings.target_bot_yc = std::stof(value);
                }
                else if (currentSection.find("WeaponRCSWeapon_") == 0) {
                    int weaponId = std::stoi(currentSection.substr(16));
                    auto& settings = all_settings::aimbot_anitaimbot::Weapon_RCS::Weapon_RCS_settings[weaponId];

                    if (key == "enabled") settings.enabled = (value == "1");
                    else if (key == "vertical") settings.vertical = std::stof(value);
                    else if (key == "horizontal") settings.horizontal = std::stof(value);
                    else if (key == "rcs_smooth") settings.rcs_smooth = std::stof(value);
                }
                else if (currentSection == "Movement") {
                    if (key == "bunny_bhop") all_settings::skin::movement::bunny_bhop = (value == "1");
                    else if (key == "auto_space_turn") all_settings::skin::movement::auto_space_turn = (value == "1");
                    else if (key == "slowwalk") all_settings::skin::movement::slowwalk = (value == "1");
                    else if (key == "slowwalk_speed") all_settings::skin::movement::slowwalk_speed = std::stof(value);
                    else if (key == "standalone_quick_stop") all_settings::skin::movement::standalone_quick_stop = (value == "1");
                }
                else if (currentSection == "Voice") {
                    if (key == "hit_sound") all_settings::skin::voice::hit_sound = (value == "1");
                    else if (key == "hit_message") all_settings::skin::voice::hit_message = (value == "1");
                    else if (key == "hit_sound_index") all_settings::skin::voice::hit_sound_index = std::stoi(value);
                    else if (key == "hit_death_message") all_settings::skin::voice::hit_death_message = (value == "1");
                    else if (key == "killerName") strcpy_s(all_settings::skin::voice::killerName, value.c_str());
                    else if (key == "weaponName") strcpy_s(all_settings::skin::voice::weaponName, value.c_str());
                }
                else if (currentSection == "Speech") {
                    if (key == "open_Speech") all_settings::skin::Speech::open_Speech = (value == "1");
                    else if (key == "Speech_mode") all_settings::skin::Speech::Speech_mode = static_cast<all_settings::skin::Speech::SpeechMode>(std::stoi(value));
                    else if (key == "savedContents") {
                        all_settings::skin::Speech::savedContents.clear();
                        if (value != "EMPTY") {
                            size_t start = 0;
                            size_t end = value.find('|');
                            while (end != std::string::npos) {
                                std::string content = value.substr(start, end - start);
                                if (!content.empty()) {
                                    all_settings::skin::Speech::savedContents.push_back(content);
                                }
                                start = end + 1;
                                end = value.find('|', start);
                            }
                            std::string lastContent = value.substr(start);
                            if (!lastContent.empty()) {
                                all_settings::skin::Speech::savedContents.push_back(lastContent);
                            }
                        }
                    }
                }
                else if (currentSection == "Menu") {
                    if (key == "menu_kjj") all_settings::skin::menu_steeing::menu_kjj = static_cast<WPARAM>(std::stoul(value));
                }
                else if (currentSection == "Visual") {
                    if (key == "ESP") all_settings::visual::ESP = (value == "1");
                    else if (key == "text_font") all_settings::visual::text_font = std::stof(value);

                }
                else if (currentSection == "EnemyVisual") {
                    if (key == "enemy_visual") all_settings::visual::enemy::enemy_visual = (value == "1");
                    else if (key == "enemy_kuang_visual") all_settings::visual::enemy::enemy_kuang_visual = (value == "1");
                    else if (key == "enemy_kuang_visual_color_ks") all_settings::visual::enemy::enemy_kuang_visual_color_ks = stringToImColor(value);
                    else if (key == "enemy_name_visual") all_settings::visual::enemy::enemy_name_visual = (value == "1");
                    else if (key == "enemy_name_visual_color") all_settings::visual::enemy::enemy_name_visual_color = stringToImColor(value);
                    else if (key == "enemy_ShowHealth_visual") all_settings::visual::enemy::enemy_ShowHealth_visual = (value == "1");
                    else if (key == "enemy_Health_color") all_settings::visual::enemy::enemy_Health_color = stringToImColor(value);
                    else if (key == "enemy_weapon_visual") all_settings::visual::enemy::enemy_weapon_visual = (value == "1");
                    else if (key == "enemy_boon_visual") all_settings::visual::enemy::enemy_boon_visual = (value == "1");
                    else if (key == "enemy_boon_visual_color") all_settings::visual::enemy::enemy_boon_visual_color = stringToImColor(value);
                    else if (key == "enemy_visual_color") all_settings::visual::enemy::enemy_visual_color = (value == "1");
                    else if (key == "enemy_weapon_glow") all_settings::visual::enemy::enemy_weapon_glow = (value == "1");
                    else if (key == "enemy_weapon_glow_color") all_settings::visual::enemy::enemy_weapon_glow_color = stringToImColor(value);
                    else if (key == "enemy_distance") all_settings::visual::enemy::enemy_distance = (value == "1");
                }
                else if (currentSection == "TeammateVisual") {
                    if (key == "teammate_visual") all_settings::visual::teammate::teammate_visual = (value == "1");
                    else if (key == "teammate_kuang_visual") all_settings::visual::teammate::teammate_kuang_visual = (value == "1");
                    else if (key == "teammate_kuang_visual_color_ks") all_settings::visual::teammate::teammate_kuang_visual_color_ks = stringToImColor(value);
                    else if (key == "teammate_name_visual") all_settings::visual::teammate::teammate_name_visual = (value == "1");
                    else if (key == "teammate_name_visual_color") all_settings::visual::teammate::teammate_name_visual_color = stringToImColor(value);
                    else if (key == "teammate_ShowHealth_visual") all_settings::visual::teammate::teammate_ShowHealth_visual = (value == "1");
                    else if (key == "teammate_Health_color") all_settings::visual::teammate::teammate_Health_color = stringToImColor(value);
                    else if (key == "teammate_weapon_visual") all_settings::visual::teammate::teammate_weapon_visual = (value == "1");
                    else if (key == "teammate_boon_visual") all_settings::visual::teammate::teammate_boon_visual = (value == "1");
                    else if (key == "teammate_boon_visual_color") all_settings::visual::teammate::teammate_boon_visual_color = stringToImColor(value);
                    else if (key == "teammate_visual_color") all_settings::visual::teammate::teammate_visual_color = (value == "1");
                    else if (key == "teammate_weapon_glow") all_settings::visual::teammate::teammate_weapon_glow = (value == "1");
                    else if (key == "teammate_weapon_glow_color") all_settings::visual::teammate::teammate_weapon_glow_color = stringToImColor(value);
                    else if (key == "teammate_distance") all_settings::visual::teammate::teammate_distance = (value == "1");
                }
                else if (currentSection == "LocalVisual") {
                    if (key == "local_glow") all_settings::visual::local::self::local_glow = (value == "1");
                    else if (key == "player_fov") all_settings::visual::local::self::player_fov = (value == "1");
                    else if (key == "player_fov_fw") all_settings::visual::local::self::player_fov_fw = std::stof(value);
                    else if (key == "weapon_glow") all_settings::visual::local::self::weapon_glow = (value == "1");
                    else if (key == "weapon_glow_color") all_settings::visual::local::self::weapon_glow_color = stringToImColor(value);
                    else if (key == "show_crosshair") all_settings::visual::local::self::show_crosshair = (value == "1");
                    else if (key == "crosshair_Length") all_settings::visual::local::self::crosshair_Length = std::stof(value);
                    else if (key == "crosshair_gap") all_settings::visual::local::self::crosshair_gap = std::stof(value);
                    else if (key == "crosshair_center_lenth") all_settings::visual::local::self::crosshair_center_lenth = std::stof(value);
                    else if (key == "crosshair_minThickness") all_settings::visual::local::self::crosshair_minThickness = std::stof(value);
                    else if (key == "crosshair_maxThickness") all_settings::visual::local::self::crosshair_maxThickness = std::stof(value);
                    else if (key == "crosshair_color_1") all_settings::visual::local::self::crosshair_color_1 = stringToImColor(value);
                    else if (key == "crosshair_color_2") all_settings::visual::local::self::crosshair_color_2 = stringToImColor(value);
                    else if (key == "Scoped_fov") all_settings::visual::local::self::Scoped_fov = std::stof(value);
                    else if (key == "remove_Scoped_fov") all_settings::visual::local::self::remove_Scoped_fov = (value == "1");
                    else if (key == "remove_Scoped_blachk") all_settings::visual::local::self::remove_Scoped_blachk = (value == "1");
                    else if (key == "ThirdPerson") all_settings::visual::local::self::ThirdPerson.enabled = (value == "1");
                    else if (key == "targe_hole_ThirdPerson") all_settings::visual::local::self::ThirdPerson.mode = static_cast<all_settings::targe_hole>(std::stoi(value));
                    else if (key == "ThirdPerson_kjj") all_settings::visual::local::self::ThirdPerson.hotkey = static_cast<WPARAM>(std::stoul(value));
                }
                else if (currentSection == "WorldVisual") {
                    if (key == "draw_world_color") all_settings::visual::local::world::draw_world_color = (value == "1");
                    else if (key == "world_color") all_settings::visual::local::world::world_color = stringToImColor(value);
                    else if (key == "draw_fov") all_settings::visual::local::world::draw_fov = (value == "1");
                    else if (key == "draw_C4_time") all_settings::visual::local::world::draw_C4_time = (value == "1");
                    else if (key == "fov_color") all_settings::visual::local::world::fov_color = stringToImColor(value);
                    else if (key == "remove_3d_skybos") all_settings::visual::local::world::remove_3d_skybos = (value == "1");
                }
                else if (currentSection == "GrenadeRemoval") {
                    if (key == "removeFlash") all_settings::visual::local::world::grenade_settings.removeFlash = (value == "1");
                    else if (key == "removeSmoke") all_settings::visual::local::world::grenade_settings.removeSmoke = (value == "1");
                    else if (key == "removeBlood") all_settings::visual::local::world::grenade_settings.removeBlood = (value == "1");
                    else if (key == "removeBulletImpacts") all_settings::visual::local::world::grenade_settings.removeBulletImpacts = (value == "1");
                    else if (key == "removeShellCasings") all_settings::visual::local::world::grenade_settings.removeShellCasings = (value == "1");
                }
                else if (currentSection == "WorldRemoval") {
                    if (key == "removeShadows") all_settings::visual::local::world::world_settings.removeShadows = (value == "1");
                    else if (key == "removeWorldLighting") all_settings::visual::local::world::world_settings.removeWorldLighting = (value == "1");
                    else if (key == "removeFog") all_settings::visual::local::world::world_settings.removeFog = (value == "1");
                    else if (key == "removeWeather") all_settings::visual::local::world::world_settings.removeWeather = (value == "1");
                    else if (key == "removeParticles") all_settings::visual::local::world::world_settings.removeParticles = (value == "1");
                }
            }
            catch (const std::exception& e) {
                std::cout << "解析配置错误: " << e.what() << " 键: " << key << " 值: " << value << std::endl;
            }
        }
    }
    file.close();
    showLoadSuccess = true;
    loadSuccessAlpha = 1.0f;
    loadSuccessTime = std::chrono::steady_clock::now();
    currentlyLoadedConfig = filename;
}

// 重置配置为默认值
void resetConfigToDefault() {
    all_settings::setting::windowscale = 1.0f;
    all_settings::aimbot_anitaimbot::aimbot::open_aimbot = {
        false,                      // enabled
        all_settings::targe_hole::ALWAYS, // mode
        0                           // hotkey
    };
    all_settings::aimbot_anitaimbot::aimbot::autofire = false;
    all_settings::aimbot_anitaimbot::aimbot::autofiree_yc = 0.0f;
    all_settings::aimbot_anitaimbot::aimbot::gqs = false;
    all_settings::aimbot_anitaimbot::aimbot::autostop = false;
    all_settings::aimbot_anitaimbot::aimbot::autoScoped = false;
    all_settings::aimbot_anitaimbot::aimbot::selected_weapon = 0;
    all_settings::aimbot_anitaimbot::aimbot::team_pd = false;
    all_settings::aimbot_anitaimbot::aimbot::current_aim_mode = all_settings::AimMode::NORMAL;

    // 重置Aimbot武器设置
    for (auto& weapon : all_settings::aimbot_anitaimbot::aimbot::aimbot_weapon_settings) {
        weapon.second = {
            false,      // enabled
            10.0f,      // fov
            0.0f,      // fov
            false,      // autowall
            10.0f,      // autowall_fw
            0,          // priority_bone
            {}          // bone_settings (全部重置为false)
        };
    }

    // 重置TargetBot设置
    all_settings::aimbot_anitaimbot::Target_Bot::target_bot = {
        false,                      // enabled
        all_settings::targe_hole::ALWAYS, // mode
        0                           // hotkey
    };
    all_settings::aimbot_anitaimbot::Target_Bot::auto_knife = false;
    all_settings::aimbot_anitaimbot::Target_Bot::auto_taser = false;

    // 重置TargetBot武器设置
    for (auto& weapon : all_settings::aimbot_anitaimbot::Target_Bot::target_bot_weapon_settings) {
        weapon.second = {
            false,  // enabled
            0.0f    // target_bot_yc
        };
    }

    // 重置Weapon_RCS设置
    all_settings::aimbot_anitaimbot::Weapon_RCS::Open_Weapon_RCS = false;

    // 重置Weapon_RCS武器设置
    for (auto& weapon : all_settings::aimbot_anitaimbot::Weapon_RCS::Weapon_RCS_settings) {
        weapon.second = {
            false,  // enabled
            1.0f,   // vertical
            1.0f,   // horizontal
            0.0f    // rcs_smooth
        };
    }    
    // 重置Movement设置
    all_settings::skin::movement::bunny_bhop = false;
    all_settings::skin::movement::auto_space_turn = false;
    all_settings::skin::movement::slowwalk = false;
    all_settings::skin::movement::slowwalk_speed = 100.0f;
    all_settings::skin::movement::standalone_quick_stop = false;

    // 重置Voice设置
    all_settings::skin::voice::hit_sound = false;
    all_settings::skin::voice::hit_message = false;
    all_settings::skin::voice::hit_sound_index = 0;
    all_settings::skin::voice::hit_death_message = false;
    strcpy_s(all_settings::skin::voice::killerName, "");
    strcpy_s(all_settings::skin::voice::weaponName, "");

    // 重置Speech设置
    all_settings::skin::Speech::open_Speech = false;
    all_settings::skin::Speech::Speech_mode = all_settings::skin::Speech::SpeechMode::ORDER;
    all_settings::skin::Speech::savedContents.clear();

    // 重置Menu设置
    all_settings::skin::menu_steeing::menu_kjj = VK_INSERT;

    // 重置Visual设置
    all_settings::visual::ESP = false;
    all_settings::visual::text_font = 0.8f;

    // 重置Enemy Visual设置
    all_settings::visual::enemy::enemy_visual = false;
    all_settings::visual::enemy::enemy_kuang_visual = false;
    all_settings::visual::enemy::enemy_kuang_visual_color_ks = ImColor(255, 0, 0, 255);
    all_settings::visual::enemy::enemy_name_visual = false;
    all_settings::visual::enemy::enemy_name_visual_color = ImColor(255, 0, 0, 255);
    all_settings::visual::enemy::enemy_ShowHealth_visual = false;
    all_settings::visual::enemy::enemy_Health_color = ImColor(255, 0, 0, 255);
    all_settings::visual::enemy::enemy_weapon_visual = false;
    all_settings::visual::enemy::enemy_boon_visual = false;
    all_settings::visual::enemy::enemy_boon_visual_color = ImColor(255, 0, 0, 255);
    all_settings::visual::enemy::enemy_visual_color = false;
    all_settings::visual::enemy::enemy_weapon_glow = false;
    all_settings::visual::enemy::enemy_weapon_glow_color = ImColor(255, 255, 255, 255);
    all_settings::visual::enemy::enemy_distance = false;

    // 重置Teammate Visual设置
    all_settings::visual::teammate::teammate_visual = false;
    all_settings::visual::teammate::teammate_kuang_visual = false;
    all_settings::visual::teammate::teammate_kuang_visual_color_ks = ImColor(0, 0, 255, 255);
    all_settings::visual::teammate::teammate_name_visual = false;
    all_settings::visual::teammate::teammate_name_visual_color = ImColor(0, 0, 255, 255);
    all_settings::visual::teammate::teammate_ShowHealth_visual = false;
    all_settings::visual::teammate::teammate_Health_color = ImColor(0, 0, 255, 255);
    all_settings::visual::teammate::teammate_weapon_visual = false;
    all_settings::visual::teammate::teammate_boon_visual = false;
    all_settings::visual::teammate::teammate_boon_visual_color = ImColor(0, 0, 255, 255);
    all_settings::visual::teammate::teammate_visual_color = false;
    all_settings::visual::teammate::teammate_weapon_glow = false;
    all_settings::visual::teammate::teammate_weapon_glow_color = ImColor(255, 255, 255, 255);
    all_settings::visual::teammate::teammate_distance = false;

    // 重置Local Visual设置
    all_settings::visual::local::self::local_glow = false;
    all_settings::visual::local::self::player_fov = false;
    all_settings::visual::local::self::player_fov_fw = 50.0f;
    all_settings::visual::local::self::weapon_glow = false;
    all_settings::visual::local::self::weapon_glow_color = ImColor(255, 255, 255, 255);
    all_settings::visual::local::self::show_crosshair = false;
    all_settings::visual::local::self::crosshair_Length = 250.0f;
    all_settings::visual::local::self::crosshair_gap = 20.0f;
    all_settings::visual::local::self::crosshair_center_lenth = 2.0f;
    all_settings::visual::local::self::crosshair_minThickness = 1.0f;
    all_settings::visual::local::self::crosshair_maxThickness = 2.5f;
    all_settings::visual::local::self::crosshair_color_1 = ImColor(255, 0, 0, 255);
    all_settings::visual::local::self::crosshair_color_2 = ImColor(0, 0, 255, 255);
    all_settings::visual::local::self::Scoped_fov = 40.0f;
    all_settings::visual::local::self::remove_Scoped_fov = false;
    all_settings::visual::local::self::remove_Scoped_blachk = false;
    all_settings::visual::local::self::ThirdPerson.enabled = false;
    all_settings::visual::local::self::ThirdPerson.mode = all_settings::targe_hole::TARGE;
    all_settings::visual::local::self::ThirdPerson.hotkey = 0;

    // 重置World Visual设置
    all_settings::visual::local::world::draw_world_color = false;
    all_settings::visual::local::world::world_color = ImColor(255, 255, 255, 255);
    all_settings::visual::local::world::draw_fov = false;
    all_settings::visual::local::world::draw_C4_time = false;
    all_settings::visual::local::world::fov_color = ImColor(255, 0, 0, 255);
    all_settings::visual::local::world::remove_3d_skybos = false;

    // 重置Grenade Removal设置
    all_settings::visual::local::world::grenade_settings.removeFlash = false;
    all_settings::visual::local::world::grenade_settings.removeSmoke = false;
    all_settings::visual::local::world::grenade_settings.removeBlood = false;
    all_settings::visual::local::world::grenade_settings.removeBulletImpacts = false;
    all_settings::visual::local::world::grenade_settings.removeShellCasings = false;

    // 重置World Removal设置
    all_settings::visual::local::world::world_settings.removeShadows = false;
    all_settings::visual::local::world::world_settings.removeWorldLighting = false;
    all_settings::visual::local::world::world_settings.removeFog = false;
    all_settings::visual::local::world::world_settings.removeWeather = false;
    all_settings::visual::local::world::world_settings.removeParticles = false;
}

// 重置选中的配置文件为默认值
void resetSelectedConfigToDefault() {
    if (selectedCfgIndex < 0 || selectedCfgIndex >= cfgFiles.size()) {
        return; // 没有选中有效的配置文件
    }

    std::string selectedFile = cfgFiles[selectedCfgIndex];
    std::string selectedFilename = selectedFile.substr(0, selectedFile.length() - 4);

    resetConfigToDefault();

    saveConfig(selectedFilename);

    loadConfig(selectedFilename);
    currentlyLoadedConfig = selectedFilename;
}